package lol.clann.minecraft.springboot.bukkit.plugin;

import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

/**
 *
 * @author pyz
 * @date 2019/4/8 10:59 PM
 */
public class AdapterContext {
    private static boolean bukkit;
    private static boolean spigot;
    private static boolean catServer;
    private static boolean forge;
    private static boolean sponge;
    private static boolean kcauldron;
    private static boolean uranium;
    private static boolean mohist;
    private static boolean paper;
    private static String packageVersion;
    @Getter
    private static final URL remappingResource;
    @Getter
    private static final boolean remapping;
    @Getter
    private static final String springBootPluginVersion;
    /**
     * 用来存储平台信息,蹦服后会打印
     */
    public static final Map<String, Object> platformInfo = new LinkedHashMap<>();

    static {
        try {
            InputStream in = AdapterContext.class.getClassLoader().getResourceAsStream("SpringBootPluginSystem.properties");
            if (in == null) {
                Adapter.getLogger().error("未读取到 SpringBootPluginSystem.properties , 可能文件损坏. 请重新下载最新版本,然后重试");
                throw new Error("未读取到 SpringBootPluginSystem.properties , 可能文件损坏. 请重新下载最新版本,然后重试");
            }
            Properties p = new Properties();
            p.load(new InputStreamReader(in, StandardCharsets.UTF_8));
            for (Object key : p.keySet()) {
                System.setProperty(key.toString(), p.getProperty((String) key));
            }
            springBootPluginVersion = p.getProperty("springbootplugin.version");
            Adapter.getLogger().info("SpringBootPluginVersion:" + springBootPluginVersion);
        } catch (Throwable var1) {
            throw new RuntimeException(var1);
        }

        catServer = isClassPresent("catserver.server.CatServer");
        platformInfo.put("catServer", catServer);
        spigot = isClassPresent("org.spigotmc.SpigotConfig");
        platformInfo.put("spigot", spigot);

        uranium = isClassPresent("cc.uraniummc.Uranium");
        platformInfo.put("uranium", uranium);

        bukkit = isClassPresent("org.bukkit.Bukkit");
        platformInfo.put("bukkit", bukkit);

        forge = isClassPresent("net.minecraftforge.common.ForgeVersion");
        platformInfo.put("forge", forge);

        sponge = isClassPresent("org.spongepowered.mod.SpongeMod");
        platformInfo.put("sponge", sponge);

        mohist = isClassPresent("red.mohist.Mohist");
        platformInfo.put("mohist", mohist);

        kcauldron = isClassPresent("kcauldron.KCauldron");
        platformInfo.put("catServer", catServer);

        // 低版本是org
        // 高版本是com
        paper = isClassPresent("com.destroystokyo.paper.PaperConfig") ||
                isClassPresent("org.github.paperspigot.PaperSpigotConfig");
        platformInfo.put("paper", paper);

        String name = Bukkit.getServer().getClass().getName();
        int dot = name.indexOf('.', "org.bukkit.craftbukkit.".length());
        if (dot > 0) {
            packageVersion = name.substring("org.bukkit.craftbukkit.".length(), dot);
        }
        platformInfo.put("packageVersion", packageVersion);
        remappingResource = findReMappingResource();
        platformInfo.put("remappingResource", remappingResource);
        remapping = remappingResource != null;
        platformInfo.put("enableRemapping", remapping);
    }

    public static void printServerContext() {
        Adapter.getLogger().error("SpringBootPlugin:" + springBootPluginVersion);
        Adapter.getLogger().error("bukkit:" + bukkit);
        Adapter.getLogger().error("spigot:" + spigot);
        Adapter.getLogger().error("paper:" + paper);
        Adapter.getLogger().error("forge:" + forge);
        Adapter.getLogger().error("kcauldron:" + kcauldron);
        Adapter.getLogger().error("uranium:" + uranium);
        Adapter.getLogger().error("catServer:" + catServer);
        Adapter.getLogger().error("mohist:" + sponge);
        Adapter.getLogger().error("sponge:" + sponge);
    }

    private static URL findReMappingResource() {
        if (packageVersion == null) {
            return null;
        }
        if (catServer) {
            return JavaPlugin.class.getClassLoader().getResource("mappings/" + packageVersion + "/cb2srg.srg");
        }
        if (mohist) {
            return JavaPlugin.class.getClassLoader().getResource("mappings/nms.srg");
        }
        if (uranium || bukkit) {
            return JavaPlugin.class.getClassLoader().getResource("mappings/" + packageVersion + "/cb2numpkg.srg");
        }
        return null;
    }

    private static boolean isClassPresent(String classname) {
        try {
            Class.forName(classname);
            return true;
        } catch (ClassNotFoundException e) {
            return false;
        }
    }

    public static String getPackageVersion() {
        return packageVersion;
    }

    public static boolean isBukkit() {
        return bukkit;
    }

    public static boolean isSpigot() {
        return spigot;
    }

    public static boolean isCatServer() {
        return catServer;
    }

    public static boolean isForge() {
        return forge;
    }

    public static boolean isSponge() {
        return sponge;
    }

    public static boolean isUranium() {
        return uranium;
    }

    public static boolean isMohist() {
        return mohist;
    }

    public static boolean isPaper() {
        return paper;
    }

    public static void init() {
    }
}
